package com.sghj.utils.Validate.password;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author xrx
 * @Date 2022/3/7
 * @apiNote
 */
public class PasswordValidator implements ConstraintValidator<Password,String> {
    //private static final String PASSWORD_PATTERN = "(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^[^\\s\\u4e00-\\u9fa5]{8,20}$";
private static final String PASSWORD_PATTERN = "^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[·\\-=【】、；‘，。/`\\[\\]\\\\;',.\\/!@#$%^&*()_+{}|:\"<>?])(?!^.*[\\u4E00-\\u9FA5].*$)^\\S{8,20}$";
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (value==null){
            return true;
        }

        if (StringUtils.isBlank(value))
        {
            return false;
        }
        return isPassword(value);
    }

    private boolean isPassword(String value) {
        Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
        Matcher matcher = pattern.matcher(value);
        boolean matches = matcher.matches();
        return matches;
    }
}
